﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DP.Sharepoint.SiteWorkflow.SSIS.SiteWorkflowActivationWS;


namespace DP.Sharepoint.SiteWorkflow.SSIS
{
    public partial class WorkflowTestStartForm : Form
    {
        public SiteWorkflowInfo WorkflowInfo { get; set; }

        public Uri SiteUrl { get; set; }

        public WorkflowTestStartForm()
        {
            InitializeComponent();
        }

        private void WorkflowTestStartForm_Load(object sender, EventArgs e)
        {
            if (WorkflowInfo == null || SiteUrl == null)
                throw new ArgumentException("Both WorkflowInfo and SiteUrl must be set before showing this form!");

            BuildGrid();

        }

        private void BuildGrid()
        {

            dataGridParams.AutoGenerateColumns = false;

            //dataGridParams.DataSource = WorkflowInfo.WorkflowParameters;

            foreach (var item in WorkflowInfo.WorkflowParameters)
            {

                dataGridParams.Rows.Add(item.Name, item.Type, string.Empty);

            }

        }

        private void btnStart_Click(object sender, EventArgs e)
        {

            try
            {
                foreach (DataGridViewRow row in dataGridParams.Rows)
                {
                    if (row.Cells[2].Value != null)
                        WorkflowInfo.WorkflowParameters.Where(x => x.Name == row.Cells[0].Value.ToString()).First().Value = row.Cells[2].Value.ToString();
                }

                DeclarativeSiteWorkflowAssistant assistant = new DeclarativeSiteWorkflowAssistant(SiteUrl);

                assistant.StartDeclarativeSiteWorkflow(WorkflowInfo);

                if (MessageBox.Show(this, "Site workflow has been started. Would you like to go to site workflow status page?", this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                {
                    OpenWorkflowStatusPage();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "Error Starting site workflow: " + ex.ToString(), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);

            }

            this.Close();
        }

        private void OpenWorkflowStatusPage()
        {
            System.Diagnostics.Process proc = new System.Diagnostics.Process();

            proc.EnableRaisingEvents = false;

            string url = SiteUrl.ToString();

            if (!url.EndsWith("/"))
                url += "/";

            proc.StartInfo.FileName = url + "_layouts/workflow.aspx";

            proc.Start();
        }

        private void dataGridParams_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {

        }



    }
}
